<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Jobs extends Model
{
    protected $table = 'dd_jobs';
    protected $primaryKey = 'id';
    public $timestamps = false;
    protected $guarded = array();
    
    /**
     * 新增工单
     * @param \Illuminate\Http\Request $request
     * @return type
     */
    public static function addJob(\Illuminate\Http\Request $request) {
        $data['plotid'] = $request->input('plotid');
        $data['cname'] = $request->input('cname');
        $data['cphone'] = $request->input('cphone');
        $data['btype'] = $request->input('btype');
        $data['address'] = $request->input('address');
        $data['jnum'] = date("YmdHis");
        $data['ctime'] = time();
        if ($request->has("ordernum")) {
            $data['ordernum'] = $request->input('ordernum');
        }
        $res = static::firstOrCreate($data);
        if ($res) {
            $mjdata['jobid'] = $res->id;
            $mjdata['iperid'] = $request->input('iperid');
            $mjdata['ctime'] = time();
            $mjres = Myjob::firstOrCreate($mjdata);
            if ($mjres) {
                $res->status = 1;
                $res->save();
                static::doSendMsg($data, $mjdata['iperid']);
                return ['status'=> 1, 'info'=>'任务创建成功，派发成功'];
            } else {
                return ['status'=> 1, 'info'=>'任务创建成功，派发失败'];
            }
        } else {
            return ['status'=>0, "info"=>"任务发布失败"];
        }
    }
    /**
     * 
     * @param type $data
     * @param type $iperid
     */
    public static function doSendMsg($data, $iperid) {
        $iperson = InstPerson::find($iperid);
        if ($iperson) {
            $content = sprintf("工单 客户姓名：%s 联系电话：%s 详细地址：%s 任务类型： %s", $data['cname'], $data['cphone'], $data['address'], $data['btype']);
            if (!empty($data['ordernum'])) {
                $content .= "用户订单号:".$data['ordernum'];
            }
            $content .="【万兆通信】";
            sendSMS(config('global.site.msg.username'), config('global.site.msg.password'), $iperson->phone, $content, config("global.site.msg.apikey"));
        }
    }
    
    
    /**
     * 工单小区
     */
    public function strPlot() {
        $plot = Plot::find($this->plotid);
        if ($plot) {
            return $plot->name;
        } else {
            return "未知小区";
        }
    }
    
    /**
     * 派单时间
     */
    public function strCtime() {
        return $this->ctime ? date("Y-m-d H:i:s", $this->ctime) : "";
    }
    
    /**
     * 工单状态
     */
    public function strJobStatus() {
        switch ($this->status) {
            case 0:
                $res = "<span class='text-danger'>未派发</span>";
                break;
            case 1:
                $res = "<span class='text-success'>已派发</span>";
                break;
            case 2:
                $res = "<span class='text-warning'>已反馈</span>";
                break;
            case 3:
                $res = "<span class='text-info'>已回单</span>";
        }
        return $res;
    }
    
    /**
     * 接单人信息
     */
    public function strReciver() {
        $recivers = Myjob::where("jobid", $this->id)
                ->leftJoin('dd_instperson', 'dd_myjob.iperid', '=', 'dd_instperson.id')
                ->select('dd_instperson.name', 'dd_instperson.phone', 'dd_myjob.status', 'dd_myjob.ctime')
                ->get();
        for ($i = 0; $i < count($recivers); $i++) {
            $recivers[$i]->ctime = date("Y-m-d H:i:s", $recivers[$i]->ctime);
        }
        if ($recivers) {
            return json_encode($recivers);
        }
    }
    /**
     * 接单人名字
     */
    public function strRecvName() {
        $recv = json_decode($this->strReciver());
        $name = array();
        foreach ($recv as $val) {
            $name[] = $val->name;
        }
        return implode('，', $name);
    }
    /**
     * 接任务时间
     * @param type $id
     */
    public function recvTime($id) {
        $mj = Myjob::where('jobid', $id)->select('ctime')->first();
        if ($mj) {
            return date("Y-m-d H:i:s", $mj->ctime);
        } else {
            return "";
        }
    }
    
    /**
     * 回单/反馈信息
     */
    public function strTick() {
        $ticks = Tick::where("jobid", $this->id)
                ->leftJoin('dd_instperson', 'dd_tick.iperid', '=', 'dd_instperson.id')
                ->leftJoin('dd_cause', 'dd_tick.causeid', '=', 'dd_cause.id')
                ->select('dd_instperson.name', 'dd_instperson.phone', 'dd_tick.*', 'dd_cause.name as causename')
                ->get();
        for ($i = 0; $i < count($ticks); $i++) {
            $ticks[$i]->btime = date("Y-m-d H:i:s", $ticks[$i]->btime);
            $ticks[$i]->recvtime = $this->recvTime($ticks[$i]->jobid);
        }
        if ($ticks) {
            return json_encode($ticks);
        }
    }
    
}
